#include <iostream>
#include <vector>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>

using namespace std;

void SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if(fl < 0)
    {
        perror("fcntl");
        return;
    }
    fcntl(fd, F_SETFL, fl | O_NONBLOCK);
    cout << "set " << fd << " nonblock" << endl;
}

int main()
{
    char buffer[1024];
    SetNonBlock(0);
    while(true)
    {
        cout << "Please Enter@";
        fflush(stdout);

        int n = read(0, buffer,sizeof(buffer)-1);
        if(n > 0)
        {
            buffer[n-1] = 0;
            cout << "echo# " << buffer << endl;
        }
        else if(n == 0)
        {
            cout << "read done" << endl;
        }
        else
        {
            if(errno == EWOULDBLOCK)
            {
                cout << "0 fd data not ready! please try again!" << endl;
                sleep(1);
            }
            else
            {
                cerr << "read error" << endl;
                break;
            }
        }
    }
    return 0;
}